package mvcdemo.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import mvcdemo.service.IUserService;
import mvcdemo.service.impl.UserServiceImpl;
import mvcdemo.vo.Merchant;
import mvcdemo.vo.User;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/user")
@SessionAttributes({"u"})//把Request的use放进session中
public class UserController {

	private IUserService service=new UserServiceImpl();
	List<Merchant> list=new ArrayList<Merchant>();
	
	@ModelAttribute("u")
	public User setUserName(String username,String password){
		System.out.println(".hello.."+username);
//		User u=new User();
//		u.setUsername(username);
//		u.setPassword(password);
		return new User();
		
	}
	
	@RequestMapping(value="/login",method=(RequestMethod.POST))
	public String login(@ModelAttribute("u") User u1/*,HttpSession session*/ /*,HttpServletRequest req*/){
		System.out.println(u1.getUsername()+"...."+u1.getPassword());
		User user=service.login(u1.getUsername(), u1.getPassword());
		if(user!=null){
			user.setPassword("");   //不想返回密码可置空
			u1=user;
			return "success";
		}
		u1.setUsername(null);
		u1.setPassword(null);
		u1=null;
		return "fail";
		
	}
	
	@RequestMapping("/showAllusers")
	@ResponseBody
	public  List<User> showAll(){
		List list=new ArrayList<>();
		list.add(new User("hello","hello"));
		list.add(new User("heather","heather"));
		list.add(new User("hehe","hehe"));
		return list;
	}
	
	@RequestMapping(value="/register1",method={RequestMethod.POST})
	public String register1(MultipartFile file) throws IllegalStateException, IOException{
		System.out.println(file.getOriginalFilename());
		File uploadfile=new File("d://0724//"+UUID.randomUUID()+file.getOriginalFilename());
		file.transferTo(uploadfile);
		return "success";
	}
	
	
	
	@RequestMapping(value="/register",method={RequestMethod.POST})
	public String register(List<MultipartFile> file,Merchant m/*ModelMap mp*/,HttpSession session) throws IllegalStateException, IOException{
		System.out.println("merchantName:"+m.getMerchantName());
		
//		for(MultipartFile f:file){
//			File uploadfile=new File("d://0724//"+UUID.randomUUID()+f.getOriginalFilename());
//			f.transferTo(uploadfile);
//			System.out.println("file name:"+f.getName());//表单域的名字
//		}
		for(int i=0;i<file.size();i++){
			MultipartFile f=file.get(i);
			File uploadfile=new File("d://0724//"+UUID.randomUUID()+f.getOriginalFilename());
			f.transferTo(uploadfile);
			if(i==0){
				m.setPic1(uploadfile.getAbsolutePath());
			}
			if(i==1){
				m.setPic2(uploadfile.getAbsolutePath());
			}
		}
//		mp.put("merchant", m);// 与ModelMap mp搭配
//		session.setAttribute("merchant", m); // 与session mp搭配
		
		list.add(m);
		session.setAttribute("list", list);
		return "redirect:getMerchant";   //重定向，调用getMerchant()
//		return "merchantsuccess";
	}
	
	@RequestMapping("/getMerchant")
	public String getMerchant(){
//		return "merchantsuccess";
		return "merchantsuccess1";
	}
	
	
	
	
}
